package cn.gene.timeyan.article.webapi.service.impl;

import cn.gene.timeyan.article.service.TopService;
import cn.gene.timeyan.article.webapi.mapper.TopMapper;
import cn.gene.timeyan.common.constant.RedisConstants;
import cn.gene.timeyan.common.utils.FormUtils;
import cn.gene.timeyan.pojo.article.vo.SimpleArticleVO;
import cn.gene.timeyan.pojo.ums.vo.UserSimpleVO;
import cn.gene.timeyan.ums.service.IUserDetailService;
import com.alibaba.fastjson.JSON;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 缃?《琛 服务实现类
 * </p>
 *
 * @author LWG
 * @since 2022-07-25
 */
@Service
public class TopServiceImpl implements TopService {
    @Autowired
    private TopMapper topMapper;
    @DubboReference
    private IUserDetailService dubboUserDetail;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public List<SimpleArticleVO> getTopList(Long modelId) {
        String key= RedisConstants.TOP_ARTICLE_LIST+"model"+modelId+":";
        List<SimpleArticleVO> list=new ArrayList<>();
        //检查缓存
        if (redisTemplate.hasKey(key+"id")){
            //有缓存,获取三条随机
            if (redisTemplate.opsForSet().size(key+"id")>=3){
                List<String> ids=redisTemplate.opsForSet().randomMembers(key+"id",3);
                for (String id:ids) {
                    SimpleArticleVO simpleArticleVO= JSON.parseObject(redisTemplate.opsForHash().get(key+"detail",id).toString(),SimpleArticleVO.class);
                    list.add(simpleArticleVO);
                }
            }else{
                Set<String> ids=redisTemplate.opsForSet().members(key+"id");
                for (String id:ids){
                    SimpleArticleVO simpleArticleVO= JSON.parseObject(redisTemplate.opsForHash().get(key+"detail",id).toString(),SimpleArticleVO.class);
                    list.add(simpleArticleVO);
                }
            }
//            if (list.size()<3){
//                throw new TimeyanServiceException(ResponseCode.INTERNAL_SERVER_ERROR,"系统繁忙");
//            }
            System.out.println("top----->list有缓存"+list);
            return list;
        }
        List<SimpleArticleVO> articles=topMapper.getArticleList(modelId);
        Map<String,Object> map=new HashMap<>();
        //存入缓存
        for (SimpleArticleVO article:articles) {
            //在展示时间范围内
            if (LocalDateTime.now().isAfter(article.getEndTime())){
                //获取文章信息
                if (article.getContent().length()>50){
                    article.setContent(article.getContent().substring(0,50)+"...");
                }else{
                    article.setContent(article.getContent());
                }
                double time= FormUtils.parseTimeByLocalDateTime(article.getCreateTime());
                //获取作者信息
                UserSimpleVO user=dubboUserDetail.selectUserCommentVOByUserId(article.getUserId());
                article.setNickName(user.getNickname());
                article.setAvatar(user.getAvatar());
                article.setIntroduction(user.getIntroduction());
                //把文章id作为value
                redisTemplate.opsForSet().add(key+"id",""+article.getArticleId());
                map.put(""+article.getArticleId(),JSON.toJSON(article));
            }
        }
        redisTemplate.opsForHash().putAll(key+"detail",map);
        //获取三条随机
        if (redisTemplate.opsForSet().size(key+"id")>=3){
            List<String> ids=redisTemplate.opsForSet().randomMembers(key+"id",3);
            for (String id:ids) {
                SimpleArticleVO simpleArticleVO= JSON.parseObject(redisTemplate.opsForHash().get(key+"detail",id).toString(),SimpleArticleVO.class);
                list.add(simpleArticleVO);
            }
        }else{
            Set<String> ids=redisTemplate.opsForSet().members(key+"id");
            for (String id:ids){
                SimpleArticleVO simpleArticleVO= JSON.parseObject(redisTemplate.opsForHash().get(key+"detail",id).toString(),SimpleArticleVO.class);
                list.add(simpleArticleVO);
            }
        }
//            if (list.size()<3){
//                throw new TimeyanServiceException(ResponseCode.INTERNAL_SERVER_ERROR,"系统繁忙");
//            }
        redisTemplate.expire(key+"id",3, TimeUnit.HOURS);
        redisTemplate.expire(key+"detail",3, TimeUnit.HOURS);
        return list;
    }

}
